package my.edu.model.information.controller;

import cn.hutool.json.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import my.edu.common.domain.PageDTO;
import my.edu.common.validate.groups.Insert;
import my.edu.common.domain.Response;
import my.edu.model.information.entity.Accounttype;
import my.edu.model.information.entity.dto.AccountSaveDTO;
import my.edu.model.information.entity.dto.AccountSearch;
import my.edu.model.information.entity.dto.CustomerSearchDTO;
import my.edu.model.information.service.IAccountService;
import my.edu.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/account")
@RestController
@Tag(name = "账户接口")
@Validated
public class AccountController {
    @Autowired
    private IAccountService accountService;

    @GetMapping("/type")
    @RequiresPermissions("account:save")
    @Operation(summary = "获取账户类型列表")
    public Response getAccountType() {
        return Response.success(accountService.getAccountType().stream().map(Accounttype::getType).toList());
    }

    @PutMapping
    @RequiresPermissions("account:save")
    @Operation(summary = "新增账户")
    public Response save(@Validated(value = Insert.class) @RequestBody AccountSaveDTO accountSaveDTO) {
        accountService.savenew(accountSaveDTO);
        return Response.success();
    }

    @GetMapping("/list")
    @RequiresPermissions("account:get")
    @Operation(summary = "获取账户列表")
    public Response getAccount(@ModelAttribute PageDTO pageDTO, AccountSearch accountSearch) {
        return Response.success(accountService.list(pageDTO,accountSearch));
    }

    @GetMapping
    @Operation(summary = "获取账户列表")
    public Response getAccount() {
        return Response.success(accountService.listOnly());
    }


    @GetMapping("/one")
    @RequiresPermissions("account:get")
    @Operation(summary = "根据编号获取账户信息")
    public Response getAccountByNo(@PathVariable @NotBlank(message = "id不能为空") String id) {
        return Response.success(accountService.getById(id));
    }

    @PostMapping("/update")
    @RequiresPermissions("account:update")
    @Operation(summary = "更新账户信息")
    public Response update(@Valid @RequestBody  AccountSaveDTO accountSaveDTO) {
        accountService.updateByDTO(accountSaveDTO);
        return Response.success();
    }

    @DeleteMapping("/delete")
    @RequiresPermissions("account:del")
    @Operation(summary = "删除账户")
    public Response delete(@NotNull(message = "id不能为空")Long id) {
        accountService.removeById(id);
        return Response.success();
    }

    @DeleteMapping("/deleteBatch")
    @RequiresPermissions("account:del")
    @Operation(summary = "删除账户批量")
    public Response deleteBatch(@NotNull(message = "id不能为空")String ids) {
        List<Long> longs = StringUtils.strToLongList(ids);
        accountService.removeByIds(longs);
        return Response.success();
    }

    @PostMapping("/batchSetStatus")
    @RequiresPermissions("account:update")
    @Operation(summary = "更新客户信息")
    public Response batchSetStatus(@RequestBody JSONObject jsonObject){
        int status = jsonObject.getInt("status");
        String ids = jsonObject.getStr("ids");
        if(status!=0&&status!=1){
            return Response.error("状态值错误");
        }
        List<Long> longs = StringUtils.strToLongList(ids);
        accountService.setStatus(longs,status);
        return Response.success();
    }


    @GetMapping("/listWithBalance")
    @RequiresPermissions("account:get")
    @Operation(summary = "根据编号获取账户信息")
    public Response getListWithBalance(@RequestParam("name") String name,
                                       @RequestParam("serialNo") String serialNo,
                                       @ModelAttribute PageDTO pageDTO) {
        return Response.success(accountService.getListWithBalance(pageDTO,name,serialNo));
    }


}
